package com.yenlien.traceyun.business.interceptor;

import com.yenlien.traceyun.common.utils.RedisUtils;
import com.yenlien.traceyun.common.crypt.TokenUtil;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-03-09
 * @description:
 **/
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtils redisUtils;

    private String[] exceptUrls = {"/yenlien/**", "/recall/**", "/deviceCenter/**", "/bigScreen/**", "/favicon.ico", "/user/login", "/user/logins", "/user/findUserByAccessToken","/user/getUserJurisdiction", "/plant/save", "/plant/getLast",
            "/plant/findByBaseSoilId", "/box/print/*", "/user/checkCode", "/re/**", "/scan/**", "/plant/scan/**", "/breed/traceinfo/**", "/traceinfo/**",
            "/companyshow/**", "/", "/error", "/management/**", "/template/**", "/file/**",
            "/error/**", "/file/qiniu/**", "/index.html", "/dist/**", "/static/**", "/css/**", "/fonts/**", "/js/**", "/coldData/**", "/fruitBigScreen/**", "/fruitApp/login", "/fruitApp/show/**",
            "/fruitApp/videoMove/**", "/iot/**", "/cold/findRentList", "/comp/findAllList", "/coldProduct/findNotPageComp", "/coldCategory/loadAllParent", "/dataAnalyse/**", "/doc.html",
            "/v2/**", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/swagger-resources/configuration/ui", "/basesoil/findAllPlot", "/storage/findPastureSort", "/storage/findByInNumAndPrice",
            "/iot/**"
    };
    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Autowired
    private CommonProperties tokenProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws BusinessException {
        //通过地址集拦截
        String url = request.getRequestURI();

//        for (String item : exceptUrls) {
//            if (url.startsWith(item)) {
//                return true;
//            }
//        }
        for (String item : exceptUrls) {
            if (antPathMatcher.match(item, url)) {
                return true;
            }
        }
        //通过注解拦截，如果不是映射到方法直接通过
        if ((handler instanceof HandlerMethod)) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //检查是否有passtoken注释，有则跳过认证
            if (method.isAnnotationPresent(PassToken.class)) {
                PassToken passToken = method.getAnnotation(PassToken.class);
                if (!passToken.required()) {
                    return true;
                }
            }
            if (method.isAnnotationPresent(DataAuth.class)) {
                DataAuth passToken = method.getAnnotation(DataAuth.class);
                request.setAttribute("auth", passToken.required());
            }
        }


        String token = request.getParameter("token");
        if (StringUtils.isEmpty(token)) {
            token = request.getHeader("token");
            if (StringUtils.isEmpty(token)) {
                throw new BusinessException("500", "未登录");
            }
        }
        TokenUtil.veryToken(token, tokenProperties.getSecret());
        return true;
    }
}
